package com.abigoni.ciaomondoandroid.bflows.activity;

import java.sql.SQLException;

import com.abigoni.ciaomondoandroid.R;
import com.abigoni.ciaomondoandroid.bflows.helper.MyDbHelper;
import com.abigoni.ciaomondoandroid.blogics.user.User;
import com.abigoni.ciaomondoandroid.blogics.user.UserService;

import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;

public class WelcomeActivity extends Activity{

	public MyDbHelper myDbHelper;
	private SQLiteDatabase db;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_welcome);

		TextView textP = (TextView) findViewById(R.id.welcome);
		Bundle bundle  = getIntent().getExtras();
		//		Button map     = (Button) findViewById(R.id.btnMap);
		//		Button dati    = (Button) findViewById(R.id.btnDati);
		//		Button events  = (Button) findViewById(R.id.buttonEvents);

		Integer idUtente        = null;
		User utente             = null;
		UserService userService = null;

		myDbHelper              = new MyDbHelper(WelcomeActivity.this);
		db                      = this.myDbHelper.getReadableDatabase();

		if(bundle != null){
			if(bundle.containsKey("userId")){
				try {
					idUtente    = (Integer) bundle.get("userId");
					userService = new UserService(db);
					utente      = userService.getUserByUserId(idUtente);

					textP.setText(textP.getText().toString() + utente.getNome());

				} catch (SQLException e) {
					e.printStackTrace();
				}	
			}
		}

		/*events.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(WelcomeActivity.this, EventsActivity.class);
				startActivity(intent);
			}
		});

		map.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				Intent intent = new Intent(WelcomeActivity.this, MapDemoActivity.class);
				startActivity(intent);
			}
		});

		dati.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				Intent intent = new Intent(WelcomeActivity.this, UpdateActivity.class);

				startActivity(intent);
			}
		});*/
	}


	public boolean onCreateOptionsMenu(Menu menu){
		/*
		 * creates a menu inflater
		 */
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()) {
		case R.id.idMainMenuPartita:
			Intent intent = new Intent(this, PartitaActivity.class);
			this.startActivity(intent);
			break;
		case R.id.idMainMenuExit:
			this.finish();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}

		return true;
	}
}